package com.ccx.demo.business.common.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.ccx.demo.config.init.AppConfig.Path;
import com.ccx.demo.config.init.AppConfig.URL;
import com.utils.IJson;
import com.utils.util.FPath;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;

/**
 * 上传文件对象
 *
 * @author 谢长春 on 2017/10/17.
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@ApiModel(description = "上传文件对象")
public class FileInfo implements Serializable, Cloneable, IJson {

    private static final long serialVersionUID = -5061171931624765053L;

    public FileInfo(final FPath.FileName fileName) {
        this.name = fileName.getName();
        this.uname = fileName.getUname();
        this.contentType = fileName.getContentType();
    }

    public static FileInfo valueOf(final FPath.FileName fileName) {
        return new FileInfo(fileName);
    }

    @JSONField(serialize = false, deserialize = false)
    @ApiModelProperty(hidden = true)
    private final String className = FileInfo.class.getSimpleName();
    @JSONField(serialize = false, deserialize = false)
    @ApiModelProperty(hidden = true)
    private String contentType;

    /**
     * 文件名，用户上传的文件名
     */
    @ApiModelProperty(position = 1, value = "文件名，用户上传的文件名", example = "测试.png", required = true)
    protected String name;
    /**
     * 唯一文件名，磁盘上存储的uuid文件名
     */
    @ApiModelProperty(position = 2, value = "唯一文件名，磁盘上存储的uuid文件名", example = "uuid.png", required = true)
    protected String uname;

    /**
     * 文件默认存在临时目录
     *
     * @return String 文件绝对路径
     */
    @ApiModelProperty(hidden = true)
    @JSONField(serialize = false, deserialize = false)
    public String getPath() {
        return StringUtils.isEmpty(uname) ? null : Path.TEMP.get(uname);
    }

    /**
     * 文件访问路径， 示例：http://127.0.0.1/app/test.xlsx
     *
     * @return String
     */
    @ApiModelProperty(position = 3, value = "文件访问路径", example = "http://127.0.0.1/app/uuid.png", accessMode = READ_ONLY)
    public String getUrl() {
        return StringUtils.isEmpty(uname) ? null : URL.TEMP.append(uname);
    }

    public <T> T parse(Class<T> clazz) {
        return JSON.parseObject(json(), clazz);
    }

    @SneakyThrows
    public FileInfo cloneObject() {
        return (FileInfo) super.clone();
    }

    public static FileInfo.FileInfoBuilder builder() {
        return new FileInfo.FileInfoBuilder();
    }

    public static class FileInfoBuilder {
        private String name;
        private String uname;

        FileInfoBuilder() {
        }

        public FileInfo.FileInfoBuilder name(String name) {
            this.name = name;
            return this;
        }

        public FileInfo.FileInfoBuilder uname(String uname) {
            this.uname = uname;
            return this;
        }

        public FileInfo build() {
            final FileInfo obj = new FileInfo();
            obj.setName(this.name);
            obj.setUname(this.uname);
            return obj;
        }

    }
}
